$(function () {
    getGradeList();
    getCorporeityList();
    allSportTable();
})


/*获取年级下拉数据*/
function getGradeList() {
    $.DemoPost("/grade/getGradePageByUserInfo?token="+sessionStorage.getItem("token"),
        {"pageNum":1,"pageSize":100},
        function (data) {
            if(data.success)
            {
                $("#gradeList").html("");
                $.each(data.obj.list,function (i,d) {
                    $("#gradeList").append("<label class='checkbox-inline'><input type='checkbox'  id='"+d.id+"'> "+d.gradeName+"</label>");
                });
            }
            else{
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("初始化数据失败");
        });
}

/*获取体质项下拉数据*/
function getCorporeityList() {
    $.DemoGet("/dictionary/getCorporeitys",
        function (data) {
            if(data.success)
            {
                $("#corporeityList").html("<option value=''>请选择</option>");
                $.each(data.obj,function (i,d) {
                    $("#corporeityList").append("<option value='"+d.id+"'>"+d.disValue+"</option>");
                });
            }
            else{
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("初始化数据失败");
        });
}


function query(params) {

    var param = {
        pageNum : params.offset / params.limit + 1,
        pageSize : params.limit
    };
    return param;
}

/*获取列表数据*/
function allSportTable() {
    $("#sport_list").bootstrapTable({
        url:$.ip+"/sportsProject/getSportsProjectPageByUserInfo?token="+sessionStorage.getItem("token"),
        method:'post',
        queryParams:query,
        sidePagination: "server",
        pagination: true,
        striped:true,
        pageNumber:1,
        pageSize: 10,
        pageList: [10],
        dataType:"json",
        contentType:"application/json",
        responseHandler:function (res) {
            return {
                total:res.obj.total,
                rows :res.obj.list
            }
        },
        columns: [
            {
                title: '序号',
                field: 'id',
                align: 'center',
                valign: 'middle',
                formatter:function (value,row,index){
                    return index+1;
                }
            },
            {
                title: '项目名称',
                align: 'center',
                field: 'projectName',
                valign: 'middle',

            } ,
            {
                title: '体质类别',
                align: 'center',
                field: 'disVal',
                valign: 'middle',

            } ,
            {
                title: '创建时间',
                align: 'center',
                field: 'crateAtStr',
                valign: 'middle',

            }  ,
            {
                title: '操作',
                align: 'center',
                field: 'id',
                width:240,
                valign: 'middle',
                formatter:function (id,row) {
                    return '<a onclick="remove(this)" data-id="'+id+'" class="btn btn-danger" href="javascript:;">删除</a> &nbsp<a onclick="update(this)" data-id="'+id+'" class="btn btn-warning " href="javascript:;">编辑</a>';
                }
            },
        ]
    });
}

function remove(obj){
    tan("是否执行当前操作？",function () {
       var id = $(obj).data("id");
       $.DemoGet("/sportsProject/delete/"+id,function (data) {
           if(data.success)
           {
               $("#sport_list").bootstrapTable('refresh');
               layer.msg(data.msg);
           }
           else {
               layer.msg(data.msg);
           }
       },function (e) {
           layer.msg("操作失败");
       });
    })

}
function add(){
    $("#name").val("");
    $("#corporeityList").val("");
    $("[type='checkbox']").each(function ()
    {
        $(this).prop("checked",false);
    });
    popdiv("tiyu-info","添加",["50%","50%"],addSport)
}

function addSport() {
    var name = $("#name").val();
    var corporeity = $("#corporeityList").val();
    var gradeList = new Array();
    $.each($('input:checkbox:checked'),function(){
        gradeList.push($(this).val())
    });
    if(name=='')
    {
        layer.msg("请输入体育项目名称");
        return;
    }
    $.DemoPost("/sportsProject/add?token="+sessionStorage.getItem("token"),
        {"projectName":name,"gradeIds":gradeList,"corporeityQuota":corporeity},
        function (data) {
            if(data.success)
            {
                layer.close(layer.index);
                $("#sport_list").bootstrapTable('refresh');
                layer.msg(data.msg);
            }
            else {
                layer.msg(data.msg);
            }
         },function (e) {
         layer.msg("操作失败");
        });
}
function update(obj){
    var id  = $(obj).data("id");
    $("#sportId").val(id);
    $.DemoGet("/sportsProject/info/"+id,function (data) {
        if(data.success)
        {
            $("#name").val(data.obj.projectName);
            $("#corporeityList").val(data.obj.dicId);
            var gradeList = data.obj.gradeList;
            $("[type='checkbox']").each(function () {

                var isCZ  = false;
               for(var i = 0;i<gradeList.length;i++)
               {
                   if($(this).attr("id")==gradeList[i]["id"])
                   {
                       isCZ = true;
                       $(this).prop("checked",true);
                       continue;
                   }
               }
               if(!isCZ)
               {
                   $(this).prop("checked",false);
               }
            })
        }
        else {
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("操作失败");
    });
    popdiv("tiyu-info","编辑",["50%","50%"],updateSport)
}

function updateSport()
{
    var name = $("#name").val();
    var corporeity = $("#corporeityList").val();
    var gradeList = new Array();
    var id = $("#sportId").val();
    $.each($('input:checkbox:checked'),function(){
        gradeList.push($(this).val())
    });
    $.DemoPost("/sportsProject/update?token="+sessionStorage.getItem("token"),
        {"id":id,"projectName":name,"gradeIds":gradeList,"corporeityQuota":corporeity},
        function (data) {
            if(data.success)
            {
                layer.close(layer.index);
                $("#sport_list").bootstrapTable('refresh');
                layer.msg(data.msg);
            }
            else {
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("操作失败");
        });
}

$("#btnQuery").click(function () {
    $("#sport_list").bootstrapTable('refresh');
});